package team.fivez.defaultmanage.dao.Impl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import team.fivez.defaultmanage.dao.RegionInfoDao;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RegionInfoDaoImpl implements RegionInfoDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public void insertInfoToDefault(String region, String year) {
        String sql="insert into region_default values(?,?)";
        jdbcTemplate.update(sql,region,year);
    }

    @Override
    public List<String> getYearListFromDefault() {
        String sql="select distinct year from region_default";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public List<String> getRegionListFromDefault() {
        String sql="select distinct region from region_default";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public Integer getCountByRegionAndYearFromDefault(String region, String year) {
        String sql="select count(*) from region_default where region=? and year=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, region, year);
    }

    @Override
    public void insertInfoToRedo(String region, String year) {
        String sql="insert into region_redo values(?,?)";
        jdbcTemplate.update(sql,region,year);
    }

    @Override
    public List<String> getYearListFromRedo() {
        String sql="select distinct year from region_redo";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public List<String> getRegionListFromRedo() {
        String sql="select distinct region from region_redo";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public Integer getCountByRegionAndYearFromRedo(String region, String year) {
        String sql="select count(*) from region_redo where region=? and year=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, region, year);
    }

    @Override
    public void deleteAllDefault() {
        String sql="delete from region_default";
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteAllRedo() {
        String sql="delete from region_redo";
        jdbcTemplate.update(sql);
    }
}
